﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace SimpleSaas.Web.Common
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
	public class ParameterFilter : IActionFilter
	{
		/// <summary>
		/// 
		/// </summary>
		private ILogger<ParameterFilter> _logger;
		private Stopwatch stopwatch;
		/// <summary>
		/// 
		/// </summary>
		/// <param name="logger"></param>
		public ParameterFilter(ILogger<ParameterFilter> logger)
		{
			_logger = logger;
			stopwatch = new Stopwatch();
		}

		/// <summary>
		/// end
		/// </summary>
		/// <param name="context"></param>
		void IActionFilter.OnActionExecuted(ActionExecutedContext context)
		{
			stopwatch.Stop();
			_logger.LogWarning($"handle time:{stopwatch.ElapsedMilliseconds} ms;");
		}

		/// <summary>
		/// start
		/// </summary>
		/// <param name="context"></param>
		void IActionFilter.OnActionExecuting(ActionExecutingContext context)
		{
			stopwatch.Start();
			var value = context.ActionArguments;
			_logger.LogWarning("==========================================");
			_logger.LogWarning($"body value{JsonConvert.SerializeObject(value)}");
			_logger.LogWarning("==========================================");
			_logger.LogWarning($"queryString:{context.HttpContext.Request.QueryString}");
			_logger.LogWarning("==========================================");
			_logger.LogWarning($"method:{context.HttpContext.Request.Method}");
			_logger.LogWarning("==========================================");
			_logger.LogWarning($"path:{context.HttpContext.Request.Path}");
			_logger.LogWarning("==========================================");

			#region todo设置全局可以访问的用户信息
			if (context.Controller is BaseController controller)
			{
				controller.SetLoginUserInfo();
			}
			#endregion

		}
	}

}